package com.xbdun.comweb.bmp.util;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.UnknownHostException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;

/**
 * @author 流打印工具
 */
public class WebCommonUtil {
	private static Logger logger = LoggerFactory.getLogger(WebCommonUtil.class);
	private static String BASE_URL;//本平台地址
	private static String WEB_VALIDATE_URL;//web-validate http连接
	/**
	* 获取当前系统的地址
	* @Title: getBaseUrl
	* @Description 获取当前系统的地址
	* @return    设定文件
	* @return String    返回类型
	 */
	public static final String getBaseUrl(){
		return WebCommonUtil.BASE_URL;
	}
	/**
	 * 获取客户端的IP地址
	 * @return
	 */
	public static final  String getIpAddr(HttpServletRequest currentRequest) {
		String ipAddress = null;
		if(null!=currentRequest){
			ipAddress = currentRequest.getHeader("x-forwarded-for");
			if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
				ipAddress = currentRequest.getHeader("Proxy-Client-IP");
			}
			if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
				ipAddress = currentRequest.getHeader("WL-Proxy-Client-IP");
			}
			if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
				ipAddress = currentRequest.getRemoteAddr();
				if (ipAddress.equals("127.0.0.1") || ipAddress.equals("0:0:0:0:0:0:0:1")) {
					// 根据网卡取本机配置的IP
					InetAddress inet = null;
					try {
						inet = InetAddress.getLocalHost();
					} catch (UnknownHostException e) {
						e.printStackTrace();
					}
					ipAddress = inet.getHostAddress();
				}
	
			}
			// 对于通过多个代理的情况，第一个IP为客户端真实IP,多个IP按照','分割
			if (ipAddress != null && ipAddress.length() > 15) {
				if (ipAddress.indexOf(",") > 0) {
					ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
				}
			}
		}
		return ipAddress;
	}
	/**
	* @Title: getLoginUrl
	* @Description: 获取登录画面
	* @return    设定文件
	* @return String    返回类型
	 */
	public static final String getLoginUrl(){
		return WebCommonUtil.BASE_URL+"system/login";
	}
	public static String getWEB_VALIDATE_URL() {
		return WEB_VALIDATE_URL;
	}
	
	/**
	 * 输出，直接输入结果数据
	 * 
	 * @param response
	 * @param result
	 */
	public final static void outWrite(HttpServletResponse response, Object result) {
		outWriteJSon(false,response,result);
	}
	
	/**
	 * HttpServletResponse 打印内容
	 * @param isJSon	   是否为
	 * @param response	HttpServletResponse
	 * @param result	传入对象
	 */
	private final static void outWriteJSon(boolean isJSon,HttpServletResponse response, Object result){
		PrintWriter out = null;
		try {
			response.setCharacterEncoding("utf-8");
			out = response.getWriter();
			if(isJSon){
				out.write(JSON.toJSONString(result));
			}else{
				out.write(result.toString());
			}
			out.close();
		} catch (IOException e) {
			logger.error("打印JSON异常",e);
		} finally {
			out.close();
		}
	}
	public final static void outWriteJSon(HttpServletResponse response, Object result){
		outWriteJSon(true,response,result);
	}
	/**
	* 设置当前系统地址
	* @Title: setBaseUrl
	* @Description 设置当前系统地址
	* @param baseUrl    设定文件
	* @return void    返回类型
	 */
	public static final void setBaseUrl(String baseUrl){
		WebCommonUtil.BASE_URL = baseUrl;
	}
	
	/**
	* @Title: setWEB_VALIDATE_URL
	* @Description: 获取通用第三方验证Web地址
	* @param wEB_VALIDATE_URL    设定文件
	* @return void    返回类型
	*/
	public static void setWEB_VALIDATE_URL(String wEB_VALIDATE_URL) {
		WebCommonUtil.WEB_VALIDATE_URL = wEB_VALIDATE_URL;
	}
	/**
	 * 打印HTML
	 * produces={"text/html;charset=UTF-8"}
	 *@param response {@link HttpServletResponse}
	 *@param content  HTML内容
	 *@throws IOException
	 */
	public static void printHtml(HttpServletResponse response, String content)throws IOException {
		response.setContentType("text/html;charset=UTF-8");
		response.getWriter().print(content);
	}
}
